qui(function (require, exports, module) {
    Program.Category = (function(){
        // 获取大分类
        function getParentCategories(data, callback) {
            var request = genCategoriesRequestStr(0);
            if (Program.Config.PCAT == null) {
                getCategories({
                    request: request
                }, function (json) {
                    Program.Config.PCAT = json;
                    callback.call(this, data);
                });
            } else {
                callback.call(this, data);
            }
        }

        function genCategoriesRequestStr(father_id) {
            var request = {};
            request.url = 'http://xinqi.if.iqiyi.com/basic/categories.json';
            request.type = 'get';
            request.params = {
                father_id: father_id
            };
            return JSON.stringify(request);
        }

        // 获取分类
        function getCategories(data, callback) {
            var options = {
                url: '/api/redirect',
                dataType: 'json',
                data: data,
                method: 'post',
                success: function (data) {
                    callback.call(this, data);
                },
                error: function (data) {
                    callback.call(this, data);
                }
            };
            $.ajax(options);
        }

        var subCategories = [];

        // 根据分类id或取对应中文
        function getSubCategories (ids, callback) {
            var self = this;
            if (ids == '') {
                callback.call(this);
                return;
            }
            var pop_id = ids.shift();
            var _id = pop_id.split('_');
            var pName = '',
                sName = '';
            var request = genCategoriesRequestStr(_id[0]);

            getCategories({
                request: request
            }, function(json) {

                for (var i = 0; i < Program.Config.PCAT.length; i++) {
                    if (Program.Config.PCAT[i].id == _id[0]) {
                        pName = Program.Config.PCAT[i].name;
                        break;
                    }
                }

                for (var i = 0; i < json.length; i++) {
                    if (json[i].id == _id[1]) {
                        sName = json[i].name;
                        break;
                    }
                }

                if (!sName)
                    subCategories.push(pName);
                else
                    subCategories.push(pName + '_' + sName);

                getSubCategories(ids, callback);
            });
        }

        function showCategoriesDlg() {

            var $container = $("#categories-dlg");

            $container.dialog({
                title: '对应列表分类',
                appendTo: null,
                modal: true,
                width: 450,
                //  height:380,
                //      maxHeight: 380,
                position: ['top', 'top+20'],
                buttons: [
                    {
                        text: '确定', //  提交分类
                        click: function () {
                            var tags = [],
                                ids = [];
                            $.each($("#category-val").find('span'), function () {
                                ids.push($(this).attr("data-tag-id"));
                                tags.push($(this).attr("data-tag-label"));
                            });
                            //console.log(ids);
                            //console.log(tags);
                            var tags = $('.tagshow').text(tags.join(','));
                            var ids = $('input[name="tag"]').val(ids.join(','));
                            $(this).dialog('close');
                        }
                    },
                    {
                        text: '取消',
                        click: function () {
                            $(this).dialog('close');
                        }
                    }
                ],

                open: function () {
                    $("#categories-dlg").find(".ui-dialog-titlebar-close").html('<span class="ui-button-icon-primary ui-icon ui-icon-closethick"></span><span class="ui-button-text">close</span>');
                },
                close: function () {

                }

            });

            var $tmpl = $("#categories");

            Program.Dao.getParentCategories('', function () {
                $container.html($tmpl.tmpl({
                    parentCategory: Program.Config.PCAT
                }));
            });


        }

        // 显示已选分类
        function _showHasSelectedCat () {
            var tags = $('.tagshow').text().split(',');
            var ids = $('input[name=tag]').val().split(',');
            var tag_span = '';
            if (tags == '') return;
            $.each(tags, function(i, item) {
                tag_span = tag_span + '<span data-tag-id="' + ids[i] + '" class="ui-suggest-value tagmanagerTag" data-tag-label="' + item + '">' + item + '<a class="tagmanagerRemoveTag"  onclick="$(this).parent().remove();"  title="Remove" href="javascript:;">x</a></span>';
            });
            $("#category-val").append(tag_span);

        }

        return {
            getParentCategories: getParentCategories,
            genCategoriesRequestStr:genCategoriesRequestStr,
            getCategories:getCategories,
            getSubCategories:getSubCategories,
            subCategories:subCategories,
            showCategoriesDlg:showCategoriesDlg,
            _showHasSelectedCat:_showHasSelectedCat
        }
    })()


});